package ru.ccfit.nsu.g7204.dronov.adPlatform;

import ru.ccfit.nsu.g7204.dronov.adPlatform.system.db.DBConnectionHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Drongo
 * Date: 18.03.11
 * Time: 20:29
 * To change this template use File | Settings | File Templates.
 */
public class ClientDBManager {
    private PreparedStatement getListStm;
    private PreparedStatement delClientStm;
    private PreparedStatement delAllStm;
    private PreparedStatement setShowingIntervalToAllStm;
    private PreparedStatement getShowingIntervalStm;

    public ClientDBManager(DBConnectionHolder dbConnectionHolder) {
        Connection conn = dbConnectionHolder.getConnection();
        try {
            delClientStm=conn.prepareStatement("call delUser(?)");
            getListStm=conn.prepareStatement("select id from users");
            delAllStm=conn.prepareStatement("delete from users");
            setShowingIntervalToAllStm =conn.prepareStatement("ALTER TABLE users CHANGE COLUMN showingInterval" +
                    " showingInterval BIGINT(20) UNSIGNED NOT NULL DEFAULT ?");
            getShowingIntervalStm=conn.prepareStatement("SELECT default(showingInterval) from users limit 1");

        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

     public void setShowingInterval(int showingInterval) {
        try {
            setShowingIntervalToAllStm.setInt(1, showingInterval);
            setShowingIntervalToAllStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public int getShowingInterval()
    {
        try {
            ResultSet rs=getShowingIntervalStm.executeQuery();
            if (rs.next())
            {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
        return 0;
    }

    public void delClient(String id)
    {
        try {
            delClientStm.setString(1,id);
            delClientStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void delAll()
    {
        try {
            delAllStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public List<String> getClients()
    {
        List<String> result=null;
        try {
            ResultSet rs=getListStm.executeQuery();
            result = new ArrayList<String>();
            for (;rs.next();)
            {
                result.add(rs.getString("id"));
            }
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
        return result;
    }
}
